package com.common.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;

import com.base.common.mvc.BaseInterceptor;
import com.base.core.mvc.core.LoginManager;
import com.base.service.system.dto.JwtUserDTO;
import com.base.service.system.service.impl.UserServiceImpl;
import com.companyname.projecttemplate.constants.CodeConstants;
import com.gitee.magic.framework.base.context.Http;
import com.gitee.magic.framework.head.exception.BusinessException;

import jakarta.servlet.http.HttpSession;

/**
 * 拦截器
 * 
 * @author start
 *
 */
public class AuthorityInterceptor extends BaseInterceptor {

	@Autowired
	private LoginManager loginManager;
	
	@Override
	public void authenticationCheck(Http http, HandlerMethod handlerMethod) {
		HttpSession session = http.getRequest().getSession();
		Object u = session.getAttribute(UserServiceImpl.USER);
		if (u == null) {
			throw new BusinessException(CodeConstants.CODE_401);
		}
		JwtUserDTO jwt=loginManager.getJwt(JwtUserDTO.class,String.valueOf(u));
		http.requestCheck(false);
        http.setAccessId(jwt.getUser().getId()+"");
		http.putCache(jwt);
	}

}
